package com.clotingshoppos.modules.order.controller;

import com.clotingshoppos.common.util.RetResponse;
import com.clotingshoppos.common.util.RetResult;
import com.clotingshoppos.common.util.StatisticsReq;
import com.clotingshoppos.modules.order.POJO.EChartResForProduct;
import com.clotingshoppos.modules.order.POJO.EChartResForStuff;
import com.clotingshoppos.modules.order.POJO.OrderRelate;
import com.clotingshoppos.modules.order.POJO.PayStatus;
import com.clotingshoppos.modules.order.entity.OrderDetailEntity;
import com.clotingshoppos.modules.order.entity.OrderEntity;
import com.clotingshoppos.modules.order.service.OrderService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName: OrderController
 * @Description: 订单功能控制器
 * @Author: Izzat
 * @Date: 2019-04-21 20:52
 * @Version: v1.0
 */
@RestController
@RequestMapping("/order")
@RequiresRoles(logical = Logical.OR, value = "admin")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @RequestMapping("/getAllOrder")
    public RetResult<Page> findAll(@RequestBody PageRequest pageRequest){
        Page page = orderService.findAll(pageRequest);
        return RetResponse.makeOKRsp(page);
    }

    @RequestMapping("/save")
    public RetResult<OrderEntity> save(@RequestBody OrderRelate orderRelate){
        OrderEntity order = orderService.save(orderRelate);
        return RetResponse.makeOKRsp(order).setMsg("订单保存成功，订单状态：未支付");
    }

    @RequestMapping("/pay")
    public RetResult<OrderEntity> pay(@RequestBody PayStatus payStatus){
        OrderEntity order = orderService.pay(payStatus);
        if(order != null){
            return RetResponse.makeOKRsp(order).setMsg("支付成功，订单状态已更新");
        }else
            return RetResponse.makeErrRsp("支付失败");
    }

    @RequestMapping("/calculate")
    public RetResult<Double> getTotalPrice(@RequestBody List<OrderDetailEntity> orderRelate){
        Double totalPrice = orderService.getTotalPrice(orderRelate);
        return  RetResponse.makeOKRsp(totalPrice).setMsg("计算成功");
    }

    @RequestMapping("/getOne")
    public RetResult<OrderRelate> getOne(@RequestBody OrderEntity orderEntity){
        OrderRelate orderRelate = orderService.findFullOrder(orderEntity);
        return RetResponse.makeOKRsp(orderRelate).setMsg("订单查找成功");
    }

    @RequestMapping("/statisticProduct")
    public RetResult<EChartResForProduct> statisticProduct(){
        EChartResForProduct statistic = orderService.CountingClothes();
        return RetResponse.makeOKRsp(statistic).setMsg("统计成功");
    }

    @RequestMapping("/statisticProductByStuff")
    public RetResult<EChartResForStuff> statisticProductByStuff(@RequestBody StatisticsReq statisticsReq){
        EChartResForStuff statistic = orderService.CountingClothesByStuff(statisticsReq.stuff,statisticsReq.timeSpan);
        return RetResponse.makeOKRsp(statistic).setMsg("统计成功");
    }

    @RequestMapping("/statisticProductWithTime")
    public RetResult<EChartResForProduct> statisticProductWithTime(@RequestBody StatisticsReq statisticsReq){
        EChartResForProduct statistic = orderService.CountingClothesWithTime(statisticsReq.timeSpan);
        return RetResponse.makeOKRsp(statistic).setMsg("统计成功");
    }

}
